package top.anonyeast;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisPoolUtils {
    private static JedisPool pool;

    static {
        try {
            //读取配置文件
            InputStream is = JedisPoolUtils.class.getResourceAsStream("jedis.properties");
            Properties pro = new Properties();
            pro.load(is);
            //获取数据，设置到JedisPoolConfig中
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
            poolConfig.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
            String host = pro.getProperty("host");
            int port = Integer.parseInt(pro.getProperty("port"));
            //初始化JedisPool
            pool = new JedisPool(poolConfig,host,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接方法
     */
    public static Jedis getJedis() {
        return pool.getResource();
    }
}
